package com.lmabbe.common.datasources.listener;

import com.alibaba.fastjson.JSONObject;
import com.lmabbe.common.datasources.config.DataSourcesProperties;
import com.lmabbe.common.datasources.handle.InitApplicationContext;
import com.lmabbe.common.datasources.handle.InitApplicationFactory;
import com.lmabbe.common.datasources.handle.InitApplicationHandle;
import com.lmabbe.common.datasources.handle.TenantInitHandle;
import com.lmabbe.common.global.context.TenantBuildContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringApplicationRunListener;
import org.springframework.context.ConfigurableApplicationContext;

import java.util.List;

/**
 * @author lmabbe
 * @data 2021/3/23 22:07
 */
@Slf4j
public class TestSpringApplicationRunListener implements SpringApplicationRunListener {


    public TestSpringApplicationRunListener(SpringApplication application, String[] args) {
        System.out.println(JSONObject.toJSONString(application));

        System.out.println(args);
    }

    /**
     * 上下文准备前调用
     *
     * @param context
     */
    @Override
    public void contextPrepared(ConfigurableApplicationContext context) {
        System.out.println("databases-TestSpringApplicationRunListener==>contextPrepared()");
    }

    /**
     * 上下文准备好了之后调用
     *
     * @param context
     */
    @Override
    public void contextLoaded(ConfigurableApplicationContext context) {
        System.out.println("databases-TestSpringApplicationRunListener==>contextLoaded()");
    }

    /**
     * Ioc创建完毕，组件都注册成功之后调用
     * 从Ioc容器之中获取所有的ApplicationRunner 和 CommonApplicationRunner 进行回调
     *
     * @param context
     */
    @Override
    public void started(ConfigurableApplicationContext context) {
        if (context.getParent() == null) {
            return;
        }
        DataSourcesProperties dataSourcesProperties = context.getBean(DataSourcesProperties.class);
        Boolean isPlatform = dataSourcesProperties.getIsPlatform();
        if (isPlatform == null || isPlatform) {
            return;
        }
        log.info("lmabbe-common-databases====>开始初始化租户信息");
        TenantInitHandle tenantInitHandle = new TenantInitHandle(context);
        List<TenantBuildContext> tenantBuildContexts = tenantInitHandle.buildContext();
        for (TenantBuildContext tenantBuildContext : tenantBuildContexts) {
            InitApplicationHandle startHandler = InitApplicationFactory.createCoreServiceChain(context, tenantBuildContext);
            startHandler.exec(new InitApplicationContext());
        }
    }


    /**
     * 运行完毕启动时调用
     *
     * @param context
     */
    @Override
    public void running(ConfigurableApplicationContext context) {
        System.out.println("databases-TestSpringApplicationRunListener==>running()");
    }

    /**
     * 启动发生异常时调用，running 和 failed 只会调用一个
     *
     * @param context
     * @param exception
     */
    @Override
    public void failed(ConfigurableApplicationContext context, Throwable exception) {
        System.out.println("databases-TestSpringApplicationRunListener==>failed()");
    }


}
